package com.wzxy.wzinfo.entiry;


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Table(name="user")
@Entity
public class User implements UserDetails{

	
	private static final long serialVersionUID = 4827676366639922670L;
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	@Column(length=30)
	private String username;
	@Column(length=30)
	private String password;
	@Column(length=2)
	private Integer type;//1提供者 ，2审批者
	@Column(name="creation_time")
	private String creationTime;
	
	public User() {
		super();
	}
	public User(Integer id, String username, String password, Integer type,
			String creationTime) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.type = type;
		this.creationTime = creationTime;
	}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}
	public String getCreationTime() {
		return creationTime;
	}
	public void setCreationTime(String creationTime) {
		this.creationTime = creationTime;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password="
				+ password + ", type=" + type + ", creationTime="
				+ creationTime + "]";
	}
	
	@Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 根据自定义逻辑来返回用户权限，如果用户权限返回空或者和拦截路径对应权限不同，验证不通过
        List<GrantedAuthority> auths = new ArrayList<>();
        // 默认所有的用户有"USER"的权利
        
        //当前角色较少不存数据库
        if(this.type==1){
        	auths.add(new SimpleGrantedAuthority("ROLE_USER"));
        }else if(this.type==2){
        	auths.add(new SimpleGrantedAuthority("ROLE_APPR"));
        }else if(this.type==3){
        	auths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        }
        return auths;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
